home *** CD-ROM | disk | FTP | other *** search
- #include <windows.h>
- #include <genstub.c>
- #include <winreg.h>
-
- #define LISTBOX_ID 1000 // Identifies the child window.
-
- EnumerateRegistry( HKEY hKey, HWND hWndListBox )
- {
- DWORD dwcSubKeys, dwcValues, dwcMaxSubKeyName, dwcMaxValueName, dwcMaxValueData;
- char szLevel[33]; // Visually demonstrates the subkey level.
- static int iLevel = 0; // Level counter
- char szBuffer[255]; // Format area for text output
- DWORD dwSubKeyIndex = 0; // Counter for subkeys
- DWORD dwValueIndex = 0; // Counter for values
- HKEY hNewKey = 0; // New key to iterate
- LONG lStatus = ERROR_SUCCESS; // Status flag for enumeration
-
- // make level string "========="
- iLevel++; // Next level now.
- FillMemory( szLevel, iLevel, '=' );
- szLevel[iLevel] = 0;
-
- // Find out how much space we need for key names and values in this subtree.
- RegQueryInfoKey( hKey, szBuffer, NULL, 0, &dwcSubKeys, &dwcMaxSubKeyName, NULL,
- &dwcValues, &dwcMaxValueName, &dwcMaxValueData, NULL, NULL );
- if ( dwcValues != 0 || dwcSubKeys != 0 )
- {
- LPTSTR lpszSubKeyName = HeapAlloc( GetProcessHeap( ), HEAP_ZERO_MEMORY,
- dwcMaxSubKeyName + 1 );
- LPTSTR lpszValueName = HeapAlloc( GetProcessHeap( ), HEAP_ZERO_MEMORY,
- dwcMaxValueName + 1 );
- LPTSTR lpszValueData = HeapAlloc( GetProcessHeap( ), HEAP_ZERO_MEMORY,
- dwcMaxValueData + 1 );
- // Enumerate values before subkeys.
- do
- {
- DWORD dwType;
- DWORD dwcValueName = dwcMaxValueName + 1;
- DWORD dwcValueData = dwcMaxValueData + 1;
-
- lStatus = RegEnumValue( hKey, dwValueIndex,lpszValueName,&dwcValueName,
- NULL, &dwType, lpszValueData, &dwcValueData );
- if ( lStatus == ERROR_SUCCESS )
- {
- wsprintf( szBuffer, "%s> VALUE [%s]=[%s], Type=%s", szLevel,
- lpszValueName, (dwType==REG_SZ) ? lpszValueData : "?",
- (dwType==REG_SZ) ? "REG_SZ" : "OTHER" );
- SendMessage( hWndListBox, LB_ADDSTRING, 0, (LPARAM) szBuffer );
- }
- dwValueIndex++;
- } while ( lStatus==ERROR_SUCCESS );
-
- // Enumerate subkeys now.
- do
- {
- DWORD dwcSubKeyName = dwcMaxSubKeyName + 1;
-
- // Get data about the sub key.
- lStatus = RegEnumKeyEx( hKey, dwSubKeyIndex, lpszSubKeyName,
- &dwcSubKeyName, NULL, NULL, NULL, NULL );
- if ( lStatus == ERROR_SUCCESS )
- {
- wsprintf( szBuffer, "%s> %3d: Key=%s", szLevel,
- dwSubKeyIndex, lpszSubKeyName );
- SendMessage( hWndListBox, LB_ADDSTRING, 0, (LPARAM) szBuffer );
- if ( RegOpenKeyEx( hKey, lpszSubKeyName, 0, KEY_ALL_ACCESS,
- &hNewKey ) == ERROR_SUCCESS )
- EnumerateRegistry( hNewKey, hWndListBox );
- RegCloseKey( hNewKey );
- }
- dwSubKeyIndex++;
- } while ( lStatus==ERROR_SUCCESS );
-
- // housekeeping - remove the data we allocated.
- HeapFree( GetProcessHeap( ), 0, lpszSubKeyName );
- HeapFree( GetProcessHeap( ), 0, lpszValueName );
- HeapFree( GetProcessHeap( ), 0, lpszValueData );
- }
- iLevel--; // Level is completed.
- }
-
- LRESULT FAR PASCAL WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- switch( uMsg )
- {
- case WM_CREATE: // Create list box child window to show registry.
- {
- RECT rectClient;
- LRESULT lRetVal = DefWindowProc(hWnd, uMsg, wParam, lParam);
- GetClientRect( hWnd, &rectClient ); // Center child in parent.
- CreateWindow( "listbox", "child listbox",
- WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER,
- 10, 10, rectClient.right - rectClient.left - 20,
- rectClient.bottom - rectClient.top - 20,
- hWnd, (HMENU) LISTBOX_ID, hInst, NULL );
- return lRetVal;
- }
- case WM_COMMAND: // process menu items
- switch( wParam )
- {
- case IDM_TEST:
- {
- HWND hWndListBox = GetDlgItem( hWnd, LISTBOX_ID );
- if ( hWndListBox )
- {
- SendMessage( hWndListBox, LB_RESETCONTENT, 0, 0 );
- EnumerateRegistry( HKEY_CLASSES_ROOT, hWndListBox );
- }
- }
- break;
- case IDM_EXIT:
- DestroyWindow( hWnd );
- break;
- }
- break;
- case WM_DESTROY:
- PostQuitMessage( 0 )
- break;
- default: // default windows message processing
- return DefWindowProc( hWnd, uMsg, wParam, lParam );
- }
- return( 0L );
- }